import router from './router'
// import { Message } from 'element-ui'
import NProgress from 'nprogress' // progress bar
import 'nprogress/nprogress.css' // progress bar style
import {getToken,removeToken} from './utils/auth'

const whiteList = ['/login', '/auth-redirect'] // no redirect whitelist
NProgress.configure({showSpinner: false}) // NProgress Configuration
// const whiteList = ['/login', '/auth-redirect'] // no redirect whitelist
import {CheckToekn} from "./api/user";

router.beforeEach(async (to, from, next) => {
    NProgress.start()
    let token = getToken();

    if (token) {
        await CheckToekn().then(response => {
            if (response.code === 200) {
                if (to.path === '/Login') {
                    next({path: '/'});
                    NProgress.done()
                }else {
                    next();
                }

            }else {
                removeToken()
                next('/login')
                NProgress.done()
            }
        });

        // next();
    } else {
        if (whiteList.indexOf(to.path) !== -1) {
            // in the free login whitelist, go directly
            next()
        } else {
            // other pages that do not have permission to access are redirected to the login page.
            next(`/login`)
            NProgress.done()
        }
    }
})

router.afterEach(() => {
    // finish progress bar
    NProgress.done()
})